package com.ourteam.product.api;

import org.apache.commons.lang.StringUtils;

import com.ourteam.frame.web.AbstractSearchModel;
import com.ourteam.product.ui.dao.IProductUIFormFieldDAO;
import com.ourteam.product.ui.dao.ProductUIFormFieldDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIFormFieldBean;

public class ProductUIFormFieldSearchModel extends AbstractSearchModel<ProductUIFormFieldDAOQueryBean> {

	private long formId;

	private long parentId;

	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public long getFormId() {
		return formId;
	}

	public void setFormId(long formId) {
		this.formId = formId;
	}

	public long getParentId() {
		return parentId;
	}

	public void setParentId(long parentId) {
		this.parentId = parentId;
	}

	@Override
	public String getMappedDAOField(String aProperty) {
		return ProductUIFormFieldBean.getMappedDAOField(aProperty);
	}

	@Override
	protected void doBuildQueryBean(ProductUIFormFieldDAOQueryBean queryBean) throws Exception {

		ProductUIFormFieldDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (this.formId != 0) {
			criteria.andFormIdEqualTo(formId);
		}

		if (this.parentId != -1) {
			criteria.andParentIdEqualTo(parentId);
		}

		if (StringUtils.isNotBlank(type)) {
			criteria.andTypeWidgetEqualTo(type);
		}

		if (queryBean.getSortProperties().isEmpty()) {
			queryBean.setSortProperty(IProductUIFormFieldDAO.SortIndex);
		}
	}

}
